import email
from django.http import JsonResponse
from django.shortcuts import render
from .models import Player,registerRecord,identifyingCodeRecord
from django.core.mail import send_mail
from django.conf import settings
import time
from django.template import Template,Context
from recordsLibrary.views import addRecord
from visualization.models import playerLog

# IdentifyingCode=dict()

# Create your views here.
def register(request):
    method=request.method
    if method=="POST":
        post=request.POST
        # 注册记录
        rr=registerRecord(
            where=request.get_host(),
            how=request.path_info,
            method=request.method,
            who=post.get('email'),
            ic=0,
            # ic=IdentifyingCode[post.get('email')],
        )
        # 判断邮箱是否已经注册
        existed=Player.objects.filter(email=post.get('email'))
        if len(existed)>0:
            rr.success=False
            addRecord(rr)
            return JsonResponse({'error':'邮箱已注册'})
            # 判断验证码是否正确
        # elif post.get('ic')!=IdentifyingCode[post.get('email')]:
        #     rr.success=False
        #     addRecord(rr)
        #     return JsonResponse({'error':'验证码错误'})
        # 通过验证
        # del IdentifyingCode[post.get('email')]
        Player(
            nickname=post.get('nickname'),
            email=post.get('email'),
            pwd=post.get('pwd'),
            headShot=request.FILES.get('headShot'),
        ).save()
        playerLog(
            email=post.get('email'),
            logins=0,
            plays=0,
            wins=0,
            witness=0,
        ).save()
        rr.success=True
        addRecord(rr)
        return JsonResponse({'status':1})
    return render(request,'register.html',{'title':'register'})

# async def getIdentifyingCode(request):
#     if request.method=='POST':
#         post=request.POST
#         ir=identifyingCodeRecord(
#             where=request.get_host(),
#             how=request.path_info,
#             method=request.method,
#             who=post.get('email'),
#             ic=IdentifyingCode[post.get('email')],
#         )
#         # 判断邮箱是否已经注册
#         existed=Player.objects.filter(email=post.get('email'))
#         if len(existed)>0:
#             ir.success=False
#             addRecord(ir)
#             return JsonResponse({'error':'邮箱已注册'})
#         IdentifyingCode[post.get('email')]=str(int(time.time()))[-6:]

#         # 渲染邮件的html页面
#         mailFile=open('register/templates/mail.html','r')
#         html=mailFile.read()
#         mailFile.close()
#         html=Template(html)
#         context=Context({
#             'title':'验证码',
#             'ic':IdentifyingCode[request.POST.get('email')],
#         })
#         html=html.render(context)
#         # 发送邮件
#         await send_mail('test','欢迎注册',settings.EMAIL_HOST_USER,[request.POST.get('email')],html_message=html)
#         # if mailReturn!=1:
#         #     ir.success=False
#         #     addRecord(ir)
#         #     return JsonResponse({'error':'邮件发送失败'})
#         ir.success=True
#         addRecord(ir)
#         return JsonResponse({'status':1})
#     return JsonResponse({'error':'method应该为POST'})
